import fs from 'fs';

function readFile(path: string): Promise<string> {
  return new Promise((res, rej) => {
    fs.readFile(path, (err, data) => {
      if (err) rej(err);
      res(data.toString());
    });
  });
}

async function readAllFile(paths: string[]) {
  let s = [];
  paths.forEach((e) => {
    s.push(readFile(e));
  });
  let a = [];
  await Promise.all(s).then((v) => {
    a.push(v);
  });
  return a;
}

async function fsReadFileAll(paths: string[]) {
  let s = [];
  for (let i = 0, size = paths.length; i < size; i++) {
    let d = await readFile(paths[i]);
    s.push(d);
  }
  return s;
}

let value = [
  './1.json',
  './2.json',
  './test.png',
  './Thoughts_TraitsSun.xml',
  '../Struct/Node.dio',
  './1.json',
  './2.json',
  './1.json',
];

async function time() {
  {
    let a = new Date().getTime();
    let d = await fsReadFileAll(value);
    let b = new Date().getTime();
    console.log(b - a);
  }
  {
    let a = new Date().getTime();
    let d = await readAllFile(value);
    let b = new Date().getTime();
    console.log(b - a);
  }
  // console.log(d);
}

time();
